package umn.angrypenguins.monopoly;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.util.ArrayList;
import umn.angrypenguins.monopoly.models.GameInstance;
import umn.angrypenguins.monopoly.models.PlayerInstance;
import umn.angrypenguins.monopoly.webservice.WebServiceClient;

public class TransferActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
    private ArrayList<PlayerInstance> players;
    private GameInstance game;
    private int playerIndex = 0;
    private PlayerInstance player;
    private Spinner spinner;

    class FindGameTask extends AsyncTask<String, String, GameInstance> {

        @Override
        protected GameInstance doInBackground(String... gameName) {
            return WebServiceClient.findGame( gameName[0] );
        }

        protected void onPostExecute(GameInstance gameInstance) {
            super.onPostExecute(gameInstance);
            
            game = gameInstance;
            initDropDown();
        }
    }
    
    class TransferTask extends AsyncTask<Integer, Integer, ArrayList<PlayerInstance>> {

        @Override
        protected ArrayList<PlayerInstance> doInBackground(Integer... amount) {
            return WebServiceClient.makeTransfer( player.getId(), players.get( playerIndex ).getId(), amount[0] );
        }

        protected void onPostExecute(ArrayList<PlayerInstance> players) {
            super.onPostExecute(players);
            
            slideBackToPlayerActivity();
        }
    }
    
    private void initDropDown() {
        players = game.getPlayers();
        
        for( PlayerInstance playerInGame: players ) {
            if( playerInGame.getId() == player.getId() ) {
                players.remove( playerInGame );
                break;
            }
        }
        
        spinner = (Spinner) findViewById(R.id.tx_to_spinner);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter( this, android.R.layout.simple_spinner_item );
        
        for( PlayerInstance playerInGame: players )
            adapter.add( playerInGame.getName() );
        
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener( this );
    }
    
    private void slideBackToPlayerActivity() {
        Bundle bundle = new Bundle();
        bundle.putSerializable( "gameInstance", game );
        bundle.putSerializable("playerInstance", player);
        Intent intent = new Intent();
        intent.setClass(this, PlayerActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transfer);
        ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Transfer Money");
        toolbarConfig.getToolbarBackButton().setText( "Cancel" );

        Bundle b = this.getIntent().getExtras();
        game = (GameInstance)b.getSerializable( "gameInstance" );
        player = (PlayerInstance) b.getSerializable("playerInstance");

        ((TextView)findViewById(R.id.tx_from_name)).setText(player.getName() + " (me)");

        findViewById(R.id.tx_money_button).setOnClickListener(this);
        findViewById(R.id.toolbar_back_button).setOnClickListener(this);
        
        new FindGameTask().execute( game.getName() );
    }

    public void onClick(View view) {
        if (view.getId() == R.id.tx_money_button) {
            new TransferTask().execute( Integer.parseInt( ( (EditText)findViewById( R.id.tx_amount_field ) ).getText().toString() ) );
        }
        else if (view.getId() == R.id.toolbar_back_button) {
            Bundle bundle = new Bundle();
            bundle.putSerializable( "gameInstance", game );
            bundle.putSerializable("playerInstance", player);
            Intent intent = new Intent();
            intent.setClass(this, PlayerActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
    
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        playerIndex = pos;
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}
